```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程同步机制解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
        }
        h1, h2, h3 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 8px;
            overflow: hidden;
        }
        .code-header {
            background: #1e2227;
            padding: 0.5rem 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .code-language {
            color: #abb2bf;
            font-size: 0.8rem;
            font-weight: 500;
        }
        .code-copy {
            color: #abb2bf;
            cursor: pointer;
            transition: all 0.2s;
        }
        .code-copy:hover {
            color: #61afef;
        }
        pre {
            margin: 0;
            padding: 1rem;
            overflow-x: auto;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .result-block {
            background: #f8f9fa;
            border-left: 4px solid #6e8efb;
            padding: 1rem;
            border-radius: 0 4px 4px 0;
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 2px;
            left: 0;
            width: 100%;
            height: 8px;
            background: rgba(110, 142, 251, 0.3);
            z-index: -1;
            transition: all 0.3s;
        }
        .highlight:hover::after {
            height: 14px;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">Java线程同步机制解析</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">探索多线程环境下数据一致性的关键解决方案</p>
            <div class="flex justify-center space-x-4">
                <div class="flex items-center">
                    <i class="fas fa-lock text-lg mr-2"></i>
                    <span>同步方法</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-code text-lg mr-2"></i>
                    <span>同步代码块</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-tachometer-alt text-lg mr-2"></i>
                    <span>性能对比</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl py-12 px-4 md:px-0">
        <!-- Problem Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                    <span>1</span>
                </div>
                <h2 class="text-2xl font-bold">问题的引出</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 mb-6">
                <p class="mb-4">在多线程环境下，当多个线程同时访问共享资源时，可能会出现数据不一致的问题。以下示例展示了四个线程同时售票时出现的负数票数问题：</p>
                
                <div class="code-block mb-6">
                    <div class="code-header">
                        <span class="code-language">Java</span>
                        <button class="code-copy"><i class="far fa-copy"></i> 复制</button>
                    </div>
                    <pre><code>public class Temp {
    public static void main(String[] args) {
        Demo dome = new Demo();
        
        new Thread(dome).start();
        new Thread(dome).start();
        new Thread(dome).start();
        new Thread(dome).start(); // 四个买票点去买五张票
    }
}

class Demo implements Runnable {
    private int ticket = 5; // 总过五张票
    
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if (ticket > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("卖票：" + ticket--);
            }
        }
    }
}</code></pre>
                </div>
                
                <div class="result-block mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2">运行结果：</h4>
                    <div class="bg-white p-3 rounded">
                        <div class="text-red-500">卖票：5</div>
                        <div class="text-red-500">卖票：4</div>
                        <div class="text-red-500">卖票：3</div>
                        <div class="text-red-500">卖票：2</div>
                        <div class="text-red-500">卖票：1</div>
                        <div class="text-red-500">卖票：0</div>
                        <div class="text-red-500">卖票：-1</div>
                        <div class="text-red-500">卖票：-2</div>
                    </div>
                </div>
                
                <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-exclamation-circle text-yellow-400"></i>
                        </div>
                        <div class="ml-3">
                            <p class="text-sm text-yellow-700">
                                从结果可以看出，票数出现了负数。这表明当一个线程正在执行减操作时，另一个线程已经将票减少了，导致了数据不一致的问题。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Solution -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                    <span>2</span>
                </div>
                <h2 class="text-2xl font-bold">解决方案：线程同步</h2>
            </div>
            
            <p class="mb-8">线程的同步有两种主要方式：<span class="highlight font-semibold">同步方法</span>和<span class="highlight font-semibold">同步代码块</span>。两者都可以实现线程同步，但性能有所不同。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <!-- Sync Method -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <i class="fas fa-lock text-blue-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">同步方法</h3>
                        </div>
                        
                        <div class="code-block mb-4">
                            <div class="code-header">
                                <span class="code-language">Java</span>
                                <button class="code-copy"><i class="far fa-copy"></i> 复制</button>
                            </div>
                            <pre><code>public class Temp {
    public static void main(String[] args) {
        Demo dome = new Demo();
        
        new Thread(dome).start();
        new Thread(dome).start();
        new Thread(dome).start();
        new Thread(dome).start();
    }
}

class Demo implements Runnable {
    private int ticket = 5;
    
    @Override
    public synchronized void run() {
        for (int i = 0; i < 10; i++) {
            if (ticket > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("卖票：" + ticket--);
            }
        }
    }
}</code></pre>
                        </div>
                        
                        <div class="result-block">
                            <h4 class="font-semibold text-gray-700 mb-2">运行结果：</h4>
                            <div class="bg-white p-3 rounded">
                                <div class="text-green-500">卖票：5</div>
                                <div class="text-green-500">卖票：4</div>
                                <div class="text-green-500">卖票：3</div>
                                <div class="text-green-500">卖票：2</div>
                                <div class="text-green-500">卖票：1</div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Sync Block -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <i class="fas fa-code text-purple-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">同步代码块</h3>
                        </div>
                        
                        <div class="code-block mb-4">
                            <div class="code-header">
                                <span class="code-language">Java</span>
                                <button class="code-copy"><i class="far fa-copy"></i> 复制</button>
                            </div>
                            <pre><code>public class Temp {
    public static void main(String[] args) {
        Demo dome = new Demo();
        
        new Thread(dome).start();
        new Thread(dome).start();
        new Thread(dome).start();
        new Thread(dome).start();
    }
}

class Demo implements Runnable {
    private int ticket = 5;
    
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            synchronized (this) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("卖票：" + ticket--);
                }
            }
        }
    }
}</code></pre>
                        </div>
                        
                        <div class="result-block">
                            <h4 class="font-semibold text-gray-700 mb-2">运行结果：</h4>
                            <div class="bg-white p-3 rounded">
                                <div class="text-green-500">卖票：5</div>
                                <div class="text-green-500">卖票：4</div>
                                <div class="text-green-500">卖票：3</div>
                                <div class="text-green-500">卖票：2</div>
                                <div class="text-green-500">卖票：1</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-blue-500 mt-1 mr-3">
                        <i class="fas fa-info-circle"></i>
                    </div>
                    <div>
                        <h3 class="font-semibold text-lg mb-2">性能比较</h3>
                        <p class="text-gray-700">虽然同步方法和同步代码块都能解决线程安全问题，但<span class="font-semibold text-blue-600">同步代码块的性能通常更高</span>，因为它只锁定必要的代码部分，而不是整个方法。这可以减少线程等待时间，提高并发性能。</p>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Visualization -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                    <span>3</span>
                </div>
                <h2 class="text-2xl font-bold">线程同步机制对比</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="mermaid">
                    graph TD
                        A[线程同步] --> B[同步方法]
                        A --> C[同步代码块]
                        B --> D[使用synchronized修饰整个方法]
                        B --> E[锁定范围较大]
                        B --> F[实现简单]
                        C --> G[使用synchronized(Object){}]
                        C --> H[锁定范围精确]
                        C --> I[性能更优]
                </div>
            </div>
        </section>
        
        <!-- Key Takeaways -->
        <section>
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                    <span>4</span>
                </div>
                <h2 class="text-2xl font-bold">关键要点</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="card bg-white rounded-xl shadow-md p-6">
                    <div class="text-blue-500 mb-3">
                        <i class="fas fa-shield-alt text-2xl"></i>
                    </div>
                    <h3 class="font-semibold text-lg mb-2">线程安全</h3>
                    <p class="text-gray-700">同步机制确保多线程环境下共享数据的一致性和正确性，避免出现竞态条件。</p>
                </div>
                
                <div class="card bg-white rounded-xl shadow-md p-6">
                    <div class="text-purple-500 mb-3">
                        <i class="fas fa-tachometer-alt text-2xl"></i>
                    </div>
                    <h3 class="font-semibold text-lg mb-2">性能考量</h3>
                    <p class="text-gray-700">同步代码块通常比同步方法性能更好，因为它减少了锁的持有时间和范围。</p>
                </div>
                
                <div class="card bg-white rounded-xl shadow-md p-6">
                    <div class="text-green-500 mb-3">
                        <i class="fas fa-lightbulb text-2xl"></i>
                    </div>
                    <h3 class="font-semibold text-lg mb-2">最佳实践</h3>
                    <p class="text-gray-700">尽量缩小同步范围，只锁定必要的代码部分，同时确保所有关键操作都被保护。</p>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">探索编程世界的知识宝库</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });

        // Copy code functionality
        document.querySelectorAll('.code-copy').forEach(button => {
            button.addEventListener('click', () => {
                const codeBlock = button.closest('.code-block').querySelector('code');
                const range = document.createRange();
                range.selectNode(codeBlock);
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
                document.execCommand('copy');
                window.getSelection().removeAllRanges();
                
                // Change icon to checkmark temporarily
                const icon = button.querySelector('i');
                icon.classList.remove('fa-copy');
                icon.classList.add('fa-check');
                
                setTimeout(() => {
                    icon.classList.remove('fa-check');
                    icon.classList.add('fa-copy');
                }, 2000);
            });
        });
    </script>
</body>
</html>
```